//
//  FollowViewController.m
//  Zaker
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FollowViewController.h"

@interface FollowViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImageView *ima;
@property (nonatomic, strong) CoreDataManager *manager;

@end

@implementation FollowViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.manager = [CoreDataManager defaults];
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Topic"];
    NSError *error;
    _dataArr = [self.manager.managedObjectContext executeFetchRequest:request error:&error].mutableCopy;
    NSLog(@"%@", _dataArr);
    if (_dataArr.count == 0) {
        [self createImaView];
    }
    
    [_tableView reloadData];
}

- (void)createImaView
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100 * HEIGHT, SCREEN_WIDTH, 30 * HEIGHT)];
    label.text = @"暂无收藏";
    label.font = [UIFont systemFontOfSize:25];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    [self.ima addSubview:label];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的关注";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.ima = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.ima.image = [UIImage imageNamed:@"暂无收藏"];
    [self.view addSubview:self.ima];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = 0;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Topic"];
    NSError *error;
    _dataArr = [self.manager.managedObjectContext executeFetchRequest:request error:&error].mutableCopy;
    Topic *model = _dataArr[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = model.title;
    cell.selectionStyle = 0;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Topic"];
    NSError *error;
    _dataArr = [self.manager.managedObjectContext executeFetchRequest:request error:&error].mutableCopy;
    Topic *top = _dataArr[indexPath.row];
    TopicModel *model = [[TopicModel alloc] init];
    model.expertId = top.expertId;
    model.alias = top.alias;
    model.picurl = top.picurl;
    model.classification = top.classification;
    model.name = top.name;
    model.headpicurl = top.headpicurl;
    model.title = top.title;
    model.questionCount = top.questionCount;
    model.answerCount = top.answerCount;
    model.expertDescription = top.expertDescription;
    TopicDetailViewController *detail = [[TopicDetailViewController alloc] init];
    detail.model = model;
    [self.navigationController pushViewController:detail animated:YES];
}

#pragma mark - 设置可进入编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark - 进入编辑态动画
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

#pragma mark - 设置为删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark - 删除cell
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Topic"];
    NSError *error;
    _dataArr = [self.manager.managedObjectContext executeFetchRequest:request error:&error].mutableCopy;
    Topic *model = _dataArr[indexPath.row];
    for (Topic *m in _dataArr) {
        if ([m.title isEqualToString:model.title]) {
            [self.manager.managedObjectContext deleteObject:m];
        }
    }
    [self.manager saveContext];
    [_dataArr removeObjectAtIndex:indexPath.row];
    if (_dataArr.count == 0) {
        [self createImaView];
    }
    [tableView reloadData];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFollow" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
